using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Editors
{
	/// <summary>
	/// Provides a user interface for browsing the properties of an object.
	/// </summary>
	internal class FrameXmlPropertyGrid : PropertyGrid
	{
		/// <summary>
		/// Sets the service provider with reflection.
		/// </summary>
		/// <param name="serviceProvider">The service provider.</param>
		public void SetServiceProvider(IServiceProvider serviceProvider)
		{
			//WORKAROUND !!!!! HACK !!!!
			//OCSO 2008.05.23.
			try
			{
				Type type = typeof (PropertyGrid);

				FieldInfo gridViewField = type.GetField("gridView", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
				Type gridViewFieldType = gridViewField.FieldType;
				var gridView = gridViewField.GetValue(this);

				FieldInfo gridViewProvider = gridViewFieldType.GetField("serviceProvider", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);

				gridViewProvider.SetValue(gridView, serviceProvider, BindingFlags.SetField | BindingFlags.NonPublic |
				                                                     BindingFlags.Instance, null, null);
			}
			catch(Exception ex)
			{
				Trace.WriteLine(ex);
			}
		}
	}
}